package com.xy;

import com.opencv.MatchResult;
import org.opencv.imgproc.Imgproc;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Random;

import static com.opencv.OpencvApi.init;
import static com.opencv.OpencvApi.matchTemplate;

/**
 * @author ：XiaoYu
 * @date ：2023/3/7 15:36
 * @version: :1.0
 * 1.启动之前设置boss自动沟通模板（记得打开自动打招呼）
 * 2.(如果已经沟通过，则不能自动打招呼，需要 CTRL+V 提前粘贴好) 如下 自己随意配置
 * 您好，请问贵公司招Java后端开发吗？需要可以查看我的简历，如果您觉得合适，可以随时联系我
 *
 * :
 */
public class Test {
    public static void main(String[] args) throws AWTException, IOException {
        init(2, null);

        Test T = new Test();
        Robot robot = new Robot();
        robot.delay(2000);

        int NumListDown = 0;
        int lock = 0;


        while (true) {
            NumListDown++;
            //设置投递多少次简历
            if (lock == 99) {
                break;
            }

            //每次循环开始,按4次下箭头 (逐行遍历)
            T.downList(4, robot);
            //因为会有偏差,需要按1次上箭头 回调位置
            if (NumListDown % 5 == 0) {
                T.upList(1, robot);
            }

            robot.delay(200);
            //获取指定矩形区域的屏幕图像(获取首页截图)
            /**
             BufferedImage bufferedImage = robot.createScreenCapture(new Rectangle(0, 300, 1920, 200~350));
             截图寻找下一页，boss有时候拉不到底，截图区域太小，就会出现卡着，无法点击下一页，无限沟通。
             这个需要动态的调整截图的高度，因为每个城市、全国的情况不一样，我也没有想到更好的办法，有厉害的兄弟可以自己优化一下
             */
            BufferedImage bufferedImage = robot.createScreenCapture(new Rectangle(0, 300, 1920, 230));
            File f = new File("images\\BossAll.png");
            OutputStream os = new FileOutputStream(f);
            ImageIO.write(bufferedImage, "png", os);
            robot.delay(500);

            //比较当前区域是否有下一页
            MatchResult pageDown = matchTemplate("images\\BossAll.png", "images\\pageDown.png", Imgproc.TM_CCORR_NORMED);
            System.out.println("pageDown下一页匹配度=====  " + pageDown.getSimilarity());
            if (pageDown.getSimilarity() > 0.996) { //有下一页

                //鼠标点击下一页
                System.out.println("鼠标点击下一页==================");
                T.mouseMovement(pageDown.getX() + 70, pageDown.getY() + 310, robot);
                System.out.println("鼠标点击下一页成功!!!==================");
                //来到下一页位置,初始化鼠标位置
                robot.mouseMove(0, 300);
                //重置去余(%)
                NumListDown = 0;
                continue;

            }

            //比较当前区域是否有沟通
            MatchResult BossChar = matchTemplate("images\\BossAll.png", "images\\chat.png", Imgproc.TM_CCORR_NORMED);
            System.out.println("BossChar首页沟通图标匹配度=====  " + BossChar.getSimilarity());

            if (BossChar.getSimilarity() > 0.999) {//有沟通


                //鼠标点击进入主页
//
                T.mouseMovement(BossChar.getX() + 200, BossChar.getY() + 300, robot);
                robot.delay(200);

                //TODO 投递简历操作
                T.goChar(robot);
                robot.delay(200);

                robot.mouseMove(0, 300);

                robot.delay(200);

                lock++;
                System.out.println("已投递简历： " + lock + " 次");


            }


        }

    }


    public void goChar(Robot robot) {
        robot.delay(1000);
        //获取指定矩形区域的屏幕图像
        BufferedImage bufferedImage = robot.createScreenCapture(new Rectangle(0, 0, 1920, 600));
        File f = new File("images\\GoOrContinue.png");
        try {
            OutputStream os = new FileOutputStream(f);
            ImageIO.write(bufferedImage, "png", os);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //比较当前区域沟通情况( 1.立即沟通   2.继续沟通)

        MatchResult goChar = matchTemplate("images\\GoOrContinue.png", "images\\GoChar.png", Imgproc.TM_CCORR_NORMED);
        System.out.println("GoChar立即沟通匹配度=====  " + goChar.getSimilarity());

        // 1.立即沟通  设置自动打招呼,可自动发送
        if (goChar.getSimilarity() > 0.999) {
            mouseMovement(goChar.getX()+30, goChar.getY() + 20, robot);
            robot.delay(3000);
            //关闭浏览器标签
            mouseMovement(470, 10, robot);
            robot.delay(1000);
            return;
        }

        //2.继续沟通   需要 发送消息
        MatchResult continueChar = matchTemplate("images\\GoOrContinue.png", "images\\continueChar.png", Imgproc.TM_CCORR_NORMED);
        System.out.println("continueChar继续沟通匹配度=====  " + continueChar.getSimilarity());
        if (continueChar.getSimilarity() > 0.999) {
            mouseMovement(goChar.getX(), goChar.getY() + 10, robot);
            robot.delay(3000);
            //TODO  发送消息   设置键盘 CTRL+C   CTRL+V

            // 以下两行按下了ctrl+v，完成粘贴功能
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.delay(50);
            // 以下释放了ctrl+v
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyRelease(KeyEvent.VK_V);
            robot.delay(50);
            // 按下回车
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.delay(50);
            //释放回车
            robot.keyRelease(KeyEvent.VK_ENTER);
            robot.delay(1000);
            //关闭浏览器标签
            mouseMovement(470, 10, robot);
            return;
        }

        //未知错误,没有找到一样关闭
        robot.delay(2000);
        mouseMovement(470, 10, robot);


    }


    //下拉列表
    public void downList(double num, Robot robot) {
        for (int i = 0; i < num; i++) {
            robot.keyPress(KeyEvent.VK_DOWN);
            robot.keyRelease(KeyEvent.VK_DOWN);
            robot.delay(50);
        }
    }

    //鼠标下拉列表
    public void downListMouse(int num, Robot robot) {
        //鼠标滚动（参数小于0，表示向上滚动；参数大于0，表示向下滚动）
        robot.mouseWheel(num);

    }

    //上滑列表
    public void upList(double num, Robot robot) {
        for (int i = 0; i < num; i++) {
            robot.keyPress(KeyEvent.VK_UP);
            robot.delay(50);
            robot.keyRelease(KeyEvent.VK_UP);
        }
    }


    //鼠标点击
    public void mouseMovement(int x, int y, Robot robot) {
        robot.mouseMove(x, y); //鼠标移动到指定位置
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);//点击鼠标左键
        robot.delay(50);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);//释放鼠标左键

    }


}
